from urllib.parse import urlencode

import time

import pymysql
import requests


def create_driver(headers, body):  # crm造网点
    create = requests.post('http://crm.meidian.com/Backstage/BusinessNode/Create', headers=headers, json=body)
    print(create.content)


def search_sql(sql, type='select'):  # mysql查询qr数据
    sql = sql  # 'select qr_uuid from growth_unmanned_shelves.meidian_qr where status = 0 limit 100;'
    conf = {
        'host': '192.168.16.18',
        'port': 6006,
        'user': 'dev',
        'password': 'jmdevcd',
        'db': 'growth_unmanned_shelves',
        'charset': 'utf8'
    }
    conn = pymysql.connect(**conf)

    cur = conn.cursor()
    cur.execute(sql)

    qr_uuids = cur.fetchall()
    conn.commit()
    # 关闭游标
    cur.close()

    # 关闭连接
    conn.close()
    return qr_uuids


def bind_qr(qr_code, shop_id):  # 接口绑定数据
    '''
    :return: 返回qr的列表
    '''
    body = {
        'address': '127.0.0.1: 8919',
        'class': 'MeiDianCrmQr',
        'func': 'bindMerchantNode',
        'value[]': '{"qrCode":"%s","merchantNodeId":"%s","type":"bind"}' % (qr_code, shop_id)
    }
    print(body)
    create_bind = requests.post('http://192.168.17.35:30303/', body)
    print(create_bind.text)
    pass


def driver_import_node(no):
    shopname = '川F-000' + str(no)
    name = '李' + str(no)
    tel = '131868740' + str(no)
    sql = "select shop_id from business_node where shop_name = '{0}' ".format(shopname)
    shop_id = search_sql(sql)[0][0]
    body = {"type": "node", "id": "{}".format(shop_id), "info": "{},,{},20180101".format(name, tel)}
    print(body)
    headers = {'Cookie': 'MDSID=1n1ofbs3124u9bg4pod8pamt62; MDSID=1n1ofbs3124u9bg4pod8pamt62'}
    resp = requests.post(url='http://crm.meidian.com/Backstage/Driver/ImportDriver', headers=headers, json=body)
    print(resp.text)


def login_activate(tel):
    body = {
        'code': '111',
        'device_id': '125E5B60637D42D4BB66ABDED12194FC',
        'mobile': '{}'.format(tel),
        'platform': 'ios',
        'client_v': '1.3801'
    }
    url = 'http://dapi.wangwangda.cn/Base/Passport/DynamicLogin'
    resp = requests.post(url=url, data=body)
    print(resp.text)


if __name__ == '__main__':
    for i in range(50, 90, 1):
        name = '李' + str(i)
        sql1 = "select id,uid from meidian_driver where name = '{}' and uid != 0 order by id desc".format(name)
        if search_sql(sql1):
            id, uid = search_sql(sql1)[0]
            print(id, uid)
            url = 'http://wangwang.hhbgrd.com/pay/shopping'
            params = {
                'type': 'copilot_a_fare',
                'shop_id': '{}'.format(uid)
            }
            url = url + '?' + urlencode(params)
            sql2 = "INSERT INTO `growth_unmanned_shelves`.`meidian_qr`(`qr_uuid`, `user_name`, `lot`, `url`, `type`, `company`, `status`, `create_time`, `update_time`) " \
                   "VALUES ('{}', 'hujunl', '1523437353_hujunl', '{}', 'copilot_a_fare', 'wangwang', 1, 1533225600, 1533225600);".format(
                uid, url)
            sql3 = "INSERT INTO growth_unmanned_shelves.meidian_business_node_qr(driver_id, type, qr_uuid, status, desction, create_time, update_time) VALUES " \
                   "({}, 1, '{}', 0, '', 1533225600, 1533225600);".format(id, uid)
            search_sql(sql2)
            search_sql(sql3)
            print(id, uid)
